// pages/me/index.js
import i18nInstance from '../../miniprogram_npm/miniprogram-i18n-plus/index'

const app = getApp()
const domain = require('../../services/domain.js')
const {
  patchUserInfo
} = require('../../services/api/user.js')
const {
  showToast
} = require('../../utils/util.js')
const {
  getAdvImg
} = require('../../services/api/common.js')
Page({
  data: {
    navs: [{
        id: 5,
        name: app.data.$language.l19,
        imgName: 'nopay'
      },
      {
        id: 6,
        name: app.data.$language.l20,
        imgName: 'noget'
      },
      {
        id: 1,
        name: app.data.$language.l21,
        imgName: 'nouse'
      },
      {
        id: 10,
        name: app.data.$language.l22,
        imgName: 'all'
      },
    ],
    languageName: '中文',
    language: 'zh',
    userinfo: {},
    offlineCustom: false,
    imgUrl: '',
  },
  onShow() {
    app.initPageLang(this)


    if (domain.token) {
      let userinfo = {
        avatar: '/assets/images/avatar.png',
        name: '*****',
      }
      if (app.data.userInfo.avatar) {
        userinfo.avatar = app.data.userInfo.avatar
      }
      if (app.data.lang == "en") {
        this.setData({
          language: "en",
          languageName: "English"
        })
      } else {
        this.setData({
          language: "zh",
          languageName: "中文"
        })
      }
      if (app.data.userInfo.name) {
        userinfo.name = app.data.userInfo.name
      }
      userinfo.account = app.data.userInfo.account
      userinfo.secretPhone = app.data.userInfo.secretPhone
      this.setData({
        userinfo
      })
    } else {}
    this.isOfflineCustom()

    this.setData({
      navs: [{
          id: 5,
          name: this.data.$language.l19,
          imgName: 'nopay'
        },
        {
          id: 6,
          name: this.data.$language.l20,
          imgName: 'noget'
        },
        {
          id: 1,
          name: this.data.$language.l21,
          imgName: 'nouse'
        },
        {
          id: 10,
          name: this.data.$language.l22,
          imgName: 'all'
        },
      ]
    })
  },
  isOfflineCustom() {
    this.setData({
      offlineCustom: app.data.offlineCustom
    })
    app.getUnmakeList(() => {
      this.setData({
        offlineCustom: app.data.offlineCustom
      })
    })
  },
  onLoad() {
    this.getAdvImg()
  },
  toLink() {
    console.log(233);
    return
    app.toTinymceView(app.data.playImgUrlData)
  },

  getAdvImg() {
    getAdvImg(10).then(res => {
      if (res.code === 0) {
        try {
          this.setData({
            imgUrl: res.info.advertInfoList[0].imgUrl
          })
        } catch (error) {

        }
      }
    })
  },
  async onSave(cb) {
    let language = ''
    if (this.data.language == 'zh') {
      language = "zh_CN"
    } else if (this.data.language == 'en') {
      language = "en_US"
    }
    const res = await patchUserInfo({
      language: language || '',
    })
    if (res.code === 0) {
      showToast(this.data.$language.l23)
      let userInfo = app.data.userInfo
      userInfo = {
        ...userInfo,
        language: this.data.language
      }
      wx.setStorage({
        key: 'language',
        data: JSON.stringify(this.data.language)
      })
      wx.setStorage({
        key: 'languageName',
        data: JSON.stringify(this.data.languageName)
      })
      wx.setStorageSync('language', this.data.language);
      i18nInstance.setLocale(this.data.language)
      app.data.lang = this.data.language
      app.initPageLang(this)
      console.log(this.data.navs, "navs");
      console.log(this.data.$language.l19);

      // 更新字体
      // wx.restartMiniProgram({
      //   path:'/pages/me/index'
      // })
      cb && cb()
    } else {
      showToast(res.tip)
    }
  },
  onChangeLangHandle() {
    wx.showActionSheet({
      itemList: ['中文', 'English'],
      success: res => {
        let lang = 'zh',
          languageName = '中文'
        if (res.tapIndex === 1) {
          lang = 'en'
          languageName = 'English'
        }
        if (this.data.language == lang) {
          return
        }
        this.setData({
          languageName,
          language: lang
        })
        this.selectComponent('.ng-navbar').setData({
          lang
        })
        this.onSave(() => {
          wx.restartMiniProgram({
            path: '/pages/me/index'
          })
          // wx.reLaunch({
          //   url: '/pages/me/index' // 目标页面的路径
          // });
        })
      }
    })
  }
})